import { createRouter, createWebHistory } from "vue-router";
import { showToast } from "vant";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 首页
    {
      path: "/",
      redirect: "home",
    },
    {
      path: "/home",
      name: "home",
      meta: {
        navshow: true,
      },
      component: () => import("../views/home.vue"),
    },
    {
      path: "/goods",
      name: "goods",
      component: () => import("../views/goods.vue"),
      //防跳
      beforeEnter(to, from, next) {
        if (localStorage.getItem("token")) {
          next();
        } else {
          next("/login");
        }
      },
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login.vue"),
      //防止重复登录
      //路由独享守卫
      beforeEnter(to, from, next) {
        if (localStorage.getItem("token")) {
          // next();
          showToast({
            message: "已登录",
            icon: "success",
          });
          next("/home");
        } else {
          next();
        }
      },
    },
    //我的
    {
      path: "/mine",
      name: "mine",
      beforeEnter(to, from, next) {
        if (localStorage.getItem("token")) {
          next();
        } else {
          next("/login");
        }
      },
      component: () => import("../views/address/mine.vue"),
    },
    //添加地址
    {
      path: "/address",
      name: "address",
      component: () => import("../views/address/address.vue"),
    },
    //订单
    {
      path: "/order",
      name: "order",
      component: () => import("../views/address/order.vue"),
    },

    //详情页,动态路由
    {
      path: "/details/:proid",
      name: "details",
      component: () => import("../views/second/detail.vue"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../views/register/register.vue"),
      children: [
        {
          path: "zhuCe1",
          name: "zhuCe1",
          component: () => import("../views/register/zhuCe1.vue"),
        },
        {
          path: "zhuCe2",
          name: "zhuCe2",
          component: () => import("../views/register/zhuCe2.vue"),
        },
        {
          path: "zhuCe3",
          name: "zhuCe3",
          component: () => import("../views/register/zhuCe3.vue"),
        },
      ],
    },
    // 搜索
    {
      path: "/search",
      name: "search",
      component: () => import("../views/second/search.vue"),
    },
  ],
});

export default router;
